package com.car.action.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.car.action.util.CommonController;
import com.car.common.model.Card;
import com.car.common.model.CardDiscuss;
import com.car.common.model.CardLike;
import com.car.common.model.Task;
import com.car.common.model.UserTasks;
import com.car.common.model.UsersInfo;
import com.car.common.model.VipRecord;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.utils.APIJSON;
import com.utils.StringUtil;
import com.utils.SystemClock;
import com.utils.Toolkit;

public class TaskController extends CommonController {

	public void get_list() {
		ArrayList<Map<String,Object>> result = Lists.newArrayList();
		Long user_id = super.getUserId();
		List<Task> taskList = Task.dao.find("select id,title,reward from task where status = 1 order by id asc ");
		boolean isall = true;
		for(Task one : taskList) {
			Map<String,Object> temp = Maps.newHashMap();
			temp.put("id", 			one.get("id"));
			temp.put("title", 		one.get("title"));
			temp.put("reward", 		"奖励:+"+ one.get("reward")+"℃");
			UserTasks utask = UserTasks.dao.findFirst("select id from user_tasks where task_type = " + one.getId() + " and user_id = " + user_id +" order by id desc limit 1 ");
			if (utask != null) {
				temp.put("status", 	3);
			} else {
				// 任务A：注册账户
				if (1 == one.getId()) {
					temp.put("status", 	1);
				// 任务B：完善基本资料
				} else if (2 == one.getId()) {
					temp.put("status", 	1);
				// 任务C：选填资料
				} else if (3 == one.getId()) {
					UsersInfo info = UsersInfo.dao.findFirst("select introduction from users_info where user_id = " + user_id);
					if (info == null || StringUtil.empty(info.getIntroduction())) {
						isall = false;
						temp.put("status", 	2);
					} else {
						temp.put("status", 	1);
					} 
					
				// 任务D：绑定QQ
				} else if (4 == one.getId()) {
					UsersInfo info = UsersInfo.dao.findFirst("select qq_openid from users_info where user_id = " + user_id);
					if (info == null || StringUtil.empty(info.getQqOpenid())) {
						isall = false;
						temp.put("status", 	2);
					} else {
						temp.put("status", 	1);
					} 
				// 任务E：绑定微信
				} else if (5 == one.getId()) {
					UsersInfo info = UsersInfo.dao.findFirst("select wx_unionid from users_info where user_id = " + user_id);
					if (info == null || StringUtil.empty(info.getWxUnionid())) {
						temp.put("status", 	2);
						isall = false;
					} else {
						temp.put("status", 	1);
					} 
				// 任务F：回复帖子
				} else if (7 == one.getId()) {
					CardDiscuss discuss = CardDiscuss.dao.findFirst("select MAX(id ) as id from card_discuss where user_id = " + user_id);
					if (discuss == null) {
						temp.put("status", 	2);
						isall = false;
					} else {
						temp.put("status", 	1);
					} 
				// 任务H：发表动态
				} else if (9 == one.getId()) {
					Card card = Card.dao.findFirst("select MAX(id ) as id from card where user_id = " + user_id);
					if (card == null) {
						temp.put("status", 	2);
						isall = false;
					} else {
						temp.put("status", 	1);
					} 
				//任务I：点赞动态
				} else if (10 == one.getId()) {
					CardLike cardlike = CardLike.dao.findFirst("select MAX(id ) as id from card where user_id = " + user_id);
					if (cardlike == null) {
						temp.put("status", 	2);
						isall = false;
					} else {
						temp.put("status", 	1);
					} 
				//任务K：全部完成额外奖励
				} else if (11 == one.getId()) {
					temp.put("status", 	isall ? 1:2);
					
				}
			}
			result.add(temp);
			
		}
		
		renderText(APIJSON.send(result));
		
	}
	
	public void receive() {
		Long user_id = super.getUserId();
		int task_id = getParaToInt("task_id"); 
		UserTasks utask = UserTasks.dao.findFirst("select id from user_tasks where task_type = " + task_id + " and user_id = " + user_id +" order by id desc limit 1 ");
		if (utask != null) {
			renderText(APIJSON.result(1046));
		} else {
			Task task = Task.dao.findById(task_id);
			if (task != null) {
				VipRecord recordMe = new VipRecord();
				recordMe.setUserId(user_id.intValue());
				recordMe.setOrderNum(Toolkit.one.newTradeNo());
				recordMe.setTitle("任务奖励" + task.getReward() + "℃");
				recordMe.setBuyType("System");
				recordMe.setHot(Integer.valueOf(task.getReward()));
				recordMe.setStatus(1);
				recordMe.setOrderType(2);
				recordMe.setCreateTime(SystemClock.now()/1000L);
				recordMe.setUpdateTime(SystemClock.now()/1000L);
				recordMe.save();
				
				utask = new UserTasks();
				utask.setUserId(user_id.intValue());
				utask.setTaskType(task_id);
				utask.setRecordId(recordMe.getId());
				utask.setCreateTime(SystemClock.now()/1000L);
				utask.setUpdateTime(SystemClock.now()/1000L);
				utask.save();
				
				Db.update("update users set hot_val = hot_val + " + task.getReward() +" where id = " + user_id);
			}
			renderText(APIJSON.result(0));
		}
	}
}
